package com.example.security.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.security.entity.SecurityUser;
import com.example.security.entity.TokenBean;
import com.example.security.service.RoleService;
import com.example.security.service.UserService;
import com.example.security.util.JwtUtil;

@RestController
@RequestMapping(value="/auth")
public class TokenController {

	@Autowired
	UserService userService;
	
	@Autowired
	RoleService roleService;
	
	@RequestMapping(value="/token")
	public TokenBean getToken(@RequestBody SecurityUser param) {
		TokenBean tkBean=new TokenBean();
		System.out.println(param);
		SecurityUser user=userService.getUserByParam(param);
		if(user!= null) {
			String token=JwtUtil.generateToken(user);
			tkBean.setAccessToken(token);
		}
		return tkBean;	
	}
	

	@ResponseBody
	@RequestMapping(value="/getUsers")
	public Map<String, Object> getUsers() {
		Map<String, Object> result=new HashMap<>();
		SecurityUser user=userService.getUserById("10010");
		result.put("user", user);
		result.put("roles", roleService.getRolesByUserId("10010"));
		return result;
			
	}
}
